﻿using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Runtime.Session;
using Mt.Site.Application.Organizations;
using Mt.Site.Application.Organizations.Dto;
using Mt.Site.Core.Common.Cache;
using Mt.Site.Core.Organizations.UserCache;
using Mt.Site.Core.Organizations.UserCache.Models;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Mt.Site.Application.Common
{
    [AbpAuthorize]
    public class UserRoleUnitSelectionAppService:MtSiteAppServiceBase,IUserRoleUnitSelectionAppService
    {
        private readonly IUserAndUnitFinder _userAndUnitFinder;
        
        public UserRoleUnitSelectionAppService(
            IUserAndUnitFinder userAndUnitFinder)
        {
            _userAndUnitFinder = userAndUnitFinder;
        }

        /// <inheritdoc/>
        public async Task<List<SimpleOrganinzationUnitDto>> GetOrganizationUnitsAsync()
        {
            var temp = await _userAndUnitFinder.GetAllUnitCacheAsync();

            var list = temp.Select(unit => new SimpleOrganinzationUnitDto {
                DisplayName = unit.DisplayName,
                ShortName = unit.Name,
                Id = unit.Id,
                ParentId = unit.ParentId,
                Code = unit.Code,
                Order = unit.Order
            }).ToList();

           
            list = OrganizationUnitHelper.AdapterList(list);

            return list.OrderBy(c=>c.Code).ToList();
        }

        /// <inheritdoc/>
        public async Task<List<SimpleOrganinzationUnitDto>> GetAllLv1UnitsAsync()
        {
            var temp = await _userAndUnitFinder.GetAllUnitCacheAsync();

            return  temp.Where(item => item.ParentId == null || item.ParentId == 0)
                .Select(unit => new SimpleOrganinzationUnitDto
                {
                    DisplayName = unit.DisplayName,
                    ShortName = unit.Name,
                    Id = unit.Id,
                    ParentId = unit.ParentId,
                    Code = unit.Code,
                    Order = unit.Order
                }).OrderBy(c => c.Code)
                .ToList();
        }


        /// <inheritdoc/> 
        public async Task<List<UnitSelectionData>> GetAllOrganizationUnitsForSelectionAsync()
        {
            return await _userAndUnitFinder.GetAllUnitSelectionData();
        }


        public async Task<UserInfoDto> GetUserInfoAsync()
        {
            return await _userAndUnitFinder.GetUserInfoAsync(AbpSession.GetUserId());
        }

        public async Task<List<UserOrganinzedCacheItem>> GetUserOrganinzationAsync()
        {
            return await _userAndUnitFinder.GetUserOrganinzedCacheAsync();
        }

        public async Task<UserInfoDto> GetUserInfoById(EntityDto<long> input)
        {
            return await _userAndUnitFinder.GetUserInfoAsync(input.Id);
        }

        public async Task<SpecialUserData> GetSpecialLeaderUserAsync()
        {
            return await _userAndUnitFinder.GetSpecialUserDataAsync();
        }
    }
}
